package com.ocom.company.controller;

import cn.hutool.core.convert.Convert;
import com.ocom.common.constant.CompanyServiceUrl;
import com.ocom.common.entity.company.EntityFlowPushInfo;
import com.ocom.common.request.company.GetPushListByFlowRequest;
import com.ocom.common.request.company.NewFlowPushRequest;
import com.ocom.common.request.company.RemoveFlowPushRequest;
import com.ocom.common.request.company.UpdateFlowPushRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.vo.company.GetPushListByFlowVo;
import com.ocom.company.service.FlowPushInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
public class FlowPushInfoController {

    @Autowired
    private FlowPushInfoService flowPushInfoService;

    @PostMapping(CompanyServiceUrl.URL_getPushListByFlow)
    public Result getPushListByFlow(@RequestBody GetPushListByFlowRequest getPushListByFlowRequest){
        EntityFlowPushInfo entityFlowPushInfo = Convert.convert(EntityFlowPushInfo.class,getPushListByFlowRequest);
        List<GetPushListByFlowVo> listByFlowVoList = new ArrayList<>();
        this.flowPushInfoService.list(getPushListByFlowRequest.getComId(),entityFlowPushInfo).forEach(entityFlowPushInfo1 -> {
            GetPushListByFlowVo getPushListByFlowVo = Convert.convert(GetPushListByFlowVo.class,entityFlowPushInfo1);
            listByFlowVoList.add(getPushListByFlowVo);
        });
        return ResultUtil.success(listByFlowVoList);
    }

    @PostMapping(CompanyServiceUrl.URL_newFlowPush)
    public Result newFlowPush(@RequestBody NewFlowPushRequest newFlowPushRequest){
        EntityFlowPushInfo entityFlowPushInfo = Convert.convert(EntityFlowPushInfo.class,newFlowPushRequest);
        return ResultUtil.success(this.flowPushInfoService.save(entityFlowPushInfo.getComId(),entityFlowPushInfo));
    }

    @PostMapping(CompanyServiceUrl.URL_updateFlowPush)
    public Result updateFlowPush(@RequestBody UpdateFlowPushRequest updateFlowPushRequest){
        EntityFlowPushInfo entityFlowPushInfo = Convert.convert(EntityFlowPushInfo.class,updateFlowPushRequest);
        return ResultUtil.success(this.flowPushInfoService.update(entityFlowPushInfo.getComId(),entityFlowPushInfo));
    }

    @PostMapping(CompanyServiceUrl.URL_removeFlowPush)
    public Result removeFlowPush(@RequestBody RemoveFlowPushRequest removeFlowPushRequest){
        EntityFlowPushInfo entityFlowPushInfo = Convert.convert(EntityFlowPushInfo.class,removeFlowPushRequest);
        return ResultUtil.success(this.flowPushInfoService.remove(entityFlowPushInfo.getComId(),entityFlowPushInfo));
    }
}
